<?php

/**
 * 特批授权服务
 * @author yuzy 2015-5-7
 */

namespace common\services\myhtsq;

use common\services\BaseService;
use common\error\ErrorCode;
use common\repositories\myhtsq\SpecialApprovalAuthorizeRepository;
use common\models\myhtsq\SpecialApprovalAuthorizeModel;
use common\services\myhtsq\DocumentService;
use common\support\PageParam;
use common\models\myhtsq\DocumentModel;
use common\support\StringHelper;
use common\services\myhtsq\ContractService;

class SpecialApprovalAuthorizeService extends BaseService {

    protected $_authorizeRepository;
        
    /**
     * 构造函数
     */
    public function __construct(SpecialApprovalAuthorizeRepository $authorizeRepository) {
        $this->_authorizeRepository = $authorizeRepository;        
    }

    /**
     * 特批授权列表
     * @param integer $page 页码
     * @param integer $pageSize 页条数
     * @param string $sortBy 排序字段
     * @param boolean $sortAsc 是否升序
     * @param integer $areaId 区域id
     * @param string $keyword 关键字
     * @return array
     */
    public function getSpecialApprovalAuthorizeList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId,$fulfil) {
        $pageParam = new PageParam($pageIndex, $pageSize, $sortBy, $sortAsc);

        #区域过滤
        if(empty($areaId)) {
           $areaId = ContractService::filterBusinessUnit(\Yii::$app->user->identity);
        }
        
        $result = SpecialApprovalAuthorizeRepository::getSpecialApprovalAuthorizeList($pageParam, $areaId, $fulfil, $keyword);
        $contractService = new ContractService();
        foreach ($result['items'] as $key => $value) {
            $applications = $contractService->getContractOrganizationInfo($value['contract_id']);
            if($applications ===FALSE){
                $applications = [];
            }
            $result['items'][$key]['applications'] = $applications; 
            unset($result['items'][$key]['specialAuthorize']);
        }
        $this->setAndReturn(ErrorCode::SUCCESS);
        return $result;
    }

    /**
     * 获取特批授权信息
     * @param string $invoiceId
     * @return \common\models\myhtsq\SpecialApprovalAuthorizeModel
     */
    public function getSpecialApprovalAuthorizeInfo($invoiceId) {
        $entity = SpecialApprovalAuthorizeRepository::getSpecialApprovalAuthorizeInfo($invoiceId);
        $list = DocumentService::getDocumentListByBusinessId($invoiceId);
        $result['item'] = $entity;
        $result['attachmentList'] = $list;
        return $result;
    }

    /**
     * 新增特批授权
     * @param \common\models\myhtsq\SpecialApprovalAuthorizeModel $authorizeModel
     * @return boolean|string   失败|合同Guid
     */
    public function addSpecialApprovalAuthorize($authorizeModel, $attachmentList) {
        if ($authorizeModel->validate()) {
            $result = $this->_authorizeRepository->addSpecialApprovalAuthorize($authorizeModel);
            if ($result === FALSE) {
                return $this->setAndReturn($this->_authorizeRepository->getErrCode());
            } else {
                // 附件
                $documentService = new DocumentService();
                foreach ($attachmentList as $idx => $value) {
                    $documentModel = new DocumentModel();
                    $documentModel->load($value,'');
                    $documentModel->document_id = StringHelper::uuid();
                    $documentModel->document_ext_name = StringHelper::getFileExtName($documentModel->document_path);
                    $documentModel->business_id = $result;
                    $documentService->addDocument($documentModel);
                }
                return $result;
            }
        } else {
            return $this->setAndReturn(ErrorCode::ERR_SPECIAL_APPROVAL_AUTHORIZE_FORM_VERIFY_FAIL);
        }
    }

    /**
     * 更新特批授权
     * @param \common\models\myhtsq\SpecialApprovalAuthorizeModel $authorizeModel
     * @return boolean|string   失败|合同Guid
     */
    public function updateSpecialApprovalAuthorize($authorizeModel, $attachmentList) {
        if ($authorizeModel->validate()) {  
            $result = $this->_authorizeRepository->updateSpecialApprovalAuthorize($authorizeModel);
            if ($result === FALSE) {
                return $this->setAndReturn($this->_authorizeRepository->getErrCode());
            } else {
                // 原附件列表
                $originalDocumentList = DocumentService::getDocumentListByBusinessId($authorizeModel->special_approval_authorize_id);
                $documentService = new DocumentService();

                foreach ($originalDocumentList as $idx => $value) {
                    $hit = false;
                    foreach ($attachmentList as $idx2 => $value2) {
                        if ($value['document_id'] == $value2['document_id']) {
                            $hit = true;
                            break;
                        }
                    }
                    if ($hit === false) {
                        $documentService->delDocumentById($value['document_id']);
                    }
                }

                foreach ($attachmentList as $idx => $value) {
                    $hit = false;
                    foreach ($originalDocumentList as $idx2 => $value2) {
                        if ($value['document_id'] == $value2['document_id']) {
                            $hit = true;
                            break;
                        }
                    }                    
                    if ($hit === false) {
                        $documentModel = new DocumentModel();
                        $documentModel->load($value,'');
                        $documentModel->document_id = StringHelper::uuid();
                        $documentModel->document_ext_name = StringHelper::getFileExtName($documentModel->document_path);
                        $documentModel->business_id = $authorizeModel->special_approval_authorize_id;
                        $documentService->addDocument($documentModel);
                    }                    

                }
                return $result;
            }
        } else {
            return $this->setAndReturn(ErrorCode::ERR_SPECIAL_APPROVAL_AUTHORIZE_FORM_VERIFY_FAIL);
        }
    }

}
